package com.br.extreme.smartstock.adapter;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.br.extreme.smartstock.R;

public class SelectAdapter extends BaseAdapter implements SpinnerAdapter{

	private Activity context;
	private List<SelectItem> list;

	public SelectAdapter(Activity context, List<SelectItem>list){
		this.context = context;
		this.list = list;
	}
	
	public Integer getItemIndex(Integer id){
		int i = 0;
		for(i = 0; i<this.list.size(); i++){
			if(this.list.get(i).getCodigo().equals(id)){
				return i;
			}
			
		}		
		return 0;		
	}
	
	@Override
	public int getCount() {

		return this.list.size();
	}

	@Override
	public Object getItem(int position) {
		return this.list.get(position);
		
	}

	@Override
	public long getItemId(int position) {
		return this.list.get(position).getCodigo();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View spinView;
		if(convertView == null){
			LayoutInflater layout = this.context.getLayoutInflater();
			spinView = layout.inflate(R.layout.custom_select, null);
		}else{
			spinView = convertView;
		}
		
		TextView txtItem = (TextView)spinView.findViewById(R.id.txtItem);
		txtItem.setText(this.list.get(position).getDescricao());
		return spinView;
	}

}
